/*
 * hlist.h
 *
 *  Created on: Nov 27, 2011
 *      Author: benoybose
 */

#ifndef HCLIST_H_
#define HCLIST_H_

typedef struct _hlist_node {
    void* data;
    struct _hlist_node* next;
    struct _hlist_node* prev;
} hclist_node;

typedef struct _hlist {
    hclist_node* head;
    hclist_node* tail;
    int count;
    const char* metadata;
} hclist;

typedef void(*hlist_foreach_function_t)(void*);

/**
 * Creats an instance of hlist.
 *
 * @param metadat A meta data that can be associated with each list
 * @return Returns an instance of the list
 */
hclist* hclist_create(const char* metadata);

/**
 * Add an item to the hlist.
 *
 * @param list Reference to the hlist instance
 * @param item Item to be added
 */
void hclist_add(hclist* list, void* item);

/**
 * Removes an item from the list.
 *
 * @param list Reference to the list instance
 * @param item Item to be removed from the list
 */
void hclist_delete(hclist* list, void* item);

/**
 * Iterate through each elements in the list.
 *
 * @param list Reference to the list instance
 * @param foreach_function Callback function that receives each item as parameter
 *
 */
void hclist_foreach(hclist* list, hlist_foreach_function_t foreach_function);

/**
 * Iterate through each elements in the list in the reverse order.
 *
 * @param list Reference to the list instance
 * @param foreach_function Callback function that receives each item as parameter
 *
 */
void hclist_foreach_r(hclist* list, hlist_foreach_function_t foreach_function);

/**
 * Search for an item in the given list.
 *
 * @param list Reference to the list instance.
 * @param item Item to be searched in the list.
 * @return Returns the index of the item. If not found returns -1
 */
int hclist_find(hclist* list, void* item);

/**
 * Counts the numer of items in the list.
 *
 * @param list Reference to the list instance
 * @return Number of items in the list
 */
int hclist_count(hclist* list);

/**
 * Gets the meta data associated with the list
 *
 * @param list Reference to the list instance
 * @return The meta data associated with the list
 */
const char* hclist_get_meatadata(hclist* list);

/**
 * Gets item at a given position.
 *
 * @param list Reference to the list instance
 * @param index Zero based position of the expected item
 *
 * @return Returns the item at position or return NULL if not found.
 */
void* hclist_get_at(hclist* list, int index);



#endif /* HCLIST_H_ */
